//
// Lexer.cpp for  in /home/lund/Projets/plazza
// 
// Made by florian dewulf
// Login   <dewulf_f@epitech.net>
// 
// Started on  Sun Apr 14 16:00:09 2013 florian dewulf
// Last update Wed Apr 17 16:44:51 2013 florian dewulf
//

#include	"Lexer.hpp"

using namespace	std;

Lexer::Lexer(const string &arg1, const string &arg2, const string &arg3)// : _str("")
{
  stringstream	ss;

  ss.str(arg1);
  ss >> this->_arg1;
  ss.clear();
  ss.str("");
  ss.str(arg2);
  ss >> this->_arg2;
  ss.clear();
  ss.str("");
  ss.str(arg3);
  ss >> this->_arg3;
  ss.clear();
  ss.str("");
}

Lexer::~Lexer()
{
}

bool		Lexer::check_pizza(const string &str)
{
  unsigned int i = 0;

  for ( ; str[i] != ' ' ; ++i)
    if (str[i] < 'A' || (str[i] > 'Z' && str[i] < 'a') || str[i] > 'z')
      return false;
  i++;
  for ( ; str[i] != ' ' ; ++i)
    if (str[i] < 'A' || (str[i] > 'Z' && str[i] < 'a') || str[i] > 'z')
      return false;
  i++;
  if (str[i] != 'x')
    return false;
  for (i += 1 ; str[i] ; ++i)
    if (str[i] < '0' || str[i] > '9')
      return false;
  return true;
}

list<CompoCmd>	Lexer::listing(const string &str)
{
  list<string>		l;
  list<CompoCmd>	lcomp;

  l = Lexer::split(str, ';');
  for (list<string>::iterator it = l.begin() ; it != l.end() ; ++it)
    {
      string		tmp;
      unsigned int	i;
      stringstream	ss;
      CompoCmd		command;

      i = it->rfind(" x") + 2;
      tmp = it->substr(i);
      ss.str(tmp);
      ss >> command.nb;
      command.cmd = it->substr(0, i - 2);
      lcomp.push_back(command);
    }
  return lcomp;
}

void		Lexer::epur_space(string &str)
{
  int		i;
  int		j;

  i = 0;
  j = 0;
  while (str[i] == ' ' || str[i] == '\t')
    i++;
  while (str[i])
    {
      if (str[i] == ' ' || str[i] == '\t')
	{
	  str[j] = ' ';
	  j++;
	  while (str[i] == ' ' || str[i] == '\t')
	    i++;
	  if (!str[i])
	    str[j - 1] = '\0';
	}
      else
	{
	  str[j] = str[i];
	  j++;
	  i++;
	}
    }
  str[j] = '\0';
}

/*void		Lexer::setStr(const string &str)
{
  this->_str = str;
  }*/

 void		Lexer::setArg1(int str)
{
  this->_arg1 = str;
}

void		Lexer::setArg2(int str)
{
  this->_arg2 = str;
}

void		Lexer::setArg3(int str)
{
  this->_arg3 = str;
  }

/*string		Lexer::getStr() const
{
  return this->_str;
  }*/

int		Lexer::getArg1() const
{
  return this->_arg1;
}

int		Lexer::getArg2() const
{
  return this->_arg2;
}

int		Lexer::getArg3() const
{
  return this->_arg3;
}

bool		Lexer::checkAv(const char *av1, const char *av2, const char *av3)
{
  for (unsigned int i = 0 ; av1[i] ; ++i)
    if (av1[i] < '0' || av1[i] > '9')
      return false;
  for (unsigned int i = 0 ; av2[i] ; ++i)
    if (av2[i] < '0' || av2[i] > '9')
      return false;
  for (unsigned int i = 0 ; av2[i] ; ++i)
    if (av2[i] < '0' || av2[i] > '9')
      return false;
  return true;
}

list<string>	Lexer::split(const string &str, char c)
{
  list<string>	l;
  unsigned int	i = 0;
  unsigned int	offset = 0;
  string	tmp;
  string	search = "";

  for (unsigned int count = 0 ; str[count] ; ++count)
    if (str[count] == c)
      i++;
  search += c;
  for (unsigned int n = 0 ; n < i ; ++n)
    {
      unsigned int	pos = str.find(search);

      tmp = str.substr(offset, pos - offset);
      l.push_back(tmp);
      offset = pos + 1;
    }
  return (l);
}
